//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

import { invoke } from "@tauri-apps/api/core";

export type EXTDATA_TYPE = number;
export const EXTDATA_TYPE_GIT: EXTDATA_TYPE = 0;     // GIT服务商接口
export const EXTDATA_TYPE_CRAWLER: EXTDATA_TYPE = 1; //地图服务商接口
export const EXTDATA_TYPE_MAP: EXTDATA_TYPE = 2;     //地图服务商接口


export type GIT_SERV_TYPE = number;
export const GIT_SERV_ATOMGIT: GIT_SERV_TYPE = 0;
export const GIT_SERV_GITCODE: GIT_SERV_TYPE = 1;
export const GIT_SERV_GITEE: GIT_SERV_TYPE = 2;
export const GIT_SERV_GITLAB: GIT_SERV_TYPE = 3;


export type CRAWLER_SERV_TYPE = number;
export const CRAWLER_SERV_FILECRAWL: CRAWLER_SERV_TYPE = 0; //firecrawl官方或私有部署


export type MAP_SERV_TYPE = number;
export const MAP_SERV_AMAP: MAP_SERV_TYPE = 0; //高德地图

export type BasicExtDataInfo = {
    name: string;
    ext_data_type: EXTDATA_TYPE;
    serv_url: string;
    access_token: string;
    serv_type: {
        GitServType?: GIT_SERV_TYPE;
        CrawlerServType?: CRAWLER_SERV_TYPE;
        MapServType?: MAP_SERV_TYPE;
    };
};

export type ExtDataInfo = {
    ext_data_id: string;
    basic_info: BasicExtDataInfo;
    create_user_id: string;
    create_display_name: string;
    create_logo_uri: string;
    create_time: number;
    update_user_id: string;
    update_display_name: string;
    update_logo_uri: string;
    update_time: number;
};

export type AddRequest = {
    session_id: string;
    board_id: string;
    basic_info: BasicExtDataInfo;
};

export type AddResponse = {
    code: number;
    err_msg: string;
    ext_data_id: string;
};

export type UpdateRequest = {
    session_id: string;
    board_id: string;
    ext_data_id: string;
    basic_info: BasicExtDataInfo;
};

export type UpdateResponse = {
    code: number;
    err_msg: string;
};

export type RemoveRequest = {
    session_id: string;
    board_id: string;
    ext_data_id: string;
};

export type RemoveResponse = {
    code: number;
    err_msg: string;
};

export type ListRequest = {
    session_id: string;
    board_id: string;
};

export type ListResponse = {
    code: number;
    err_msg: string;
    ext_data_list: ExtDataInfo[];
};

export type GetRequest = {
    session_id: string;
    board_id: string;
    ext_data_id: string;
};

export type GetResponse = {
    code: number;
    err_msg: string;
    ext_data: ExtDataInfo;
};

// 增加外部数据服务
export async function add(request: AddRequest): Promise<AddResponse> {
    const cmd = 'board_extdata_api_cmds_add';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<AddResponse>(cmd, {
        request,
    });
}

// 更新外部数据服务
export async function update(request: UpdateRequest): Promise<UpdateResponse> {
    const cmd = 'board_extdata_api_cmds_update';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateResponse>(cmd, {
        request,
    });
}

// 删除外部数据服务
export async function remove(request: RemoveRequest): Promise<RemoveResponse> {
    const cmd = 'board_extdata_api_cmds_remove';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<RemoveResponse>(cmd, {
        request,
    });
}

// 列出外部服务数据服务
export async function list(request: ListRequest): Promise<ListResponse> {
    const cmd = 'board_extdata_api_cmds_list';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListResponse>(cmd, {
        request,
    });
}

// 获取单个外部数据服务
export async function get(request: GetRequest): Promise<GetResponse> {
    const cmd = 'board_extdata_api_cmds_get';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<GetResponse>(cmd, {
        request,
    });
}